package so.hands.xam.core; 

import java.io.File;
import java.io.IOException;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

import com.thoughtworks.selenium.Selenium;

/** 
 * @Title: TestRunWebDriver.java  
 * @Package so.hands.xam.core 
 * @Description: TODO(添加描述) 
 * @author 朱晓峰 
 * @date 2012-11-1 下午10:16:32 
 * @version V1.0 
 */
public class TestRunWebDriver {

	@SuppressWarnings("unused")
	private static final String[] BROWSER_TYPE_ARRAY = {"firefox","ie","chrome","safari"};
	private static final String BROWSER_TOOLS_PATH = "./src/main/resources/bspath/";
	
	private String browserType;
	private WebDriver browserDriver;
	private Selenium selenium;
	private String appTestUrl;
	
	
	/**
	 * @Title TestRunWebDriver(String appTestUrl)
	 * @Description 无参数传入时，默认用IE测试
	 * @param appTestUrl
	 * @throws IOException 
	 */
	public TestRunWebDriver(String appTestUrl) throws IOException{
		 String bsPath = BROWSER_TOOLS_PATH + "IEDriverServer.exe";
		 System.setProperty("webdriver.ie.driver", bsPath);
		 DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
		 capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
		 this.browserDriver = new InternetExplorerDriver(capabilities);
		 initSelenium(appTestUrl);
	}
	
	/**
	 * @Title TestRunWebDriver(String browserType,String appTestUrl,String browserPath)
	 * @Description 指定浏览器类型启动对应浏览器
	 * @param browserType
	 * @param appTestUrl
	 * @param browserPath
	 */
	public TestRunWebDriver(String browserType,String appTestUrl,String browserPath){
		String bsPath = BROWSER_TOOLS_PATH;
		try{
			if(browserType.equals("firefox")){
				File pathToFirefoxBinary = new File(browserPath);
				FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);
				this.browserDriver = new FirefoxDriver(firefoxbin,null);
			}else if(browserType.equals("ie")){
				bsPath = bsPath + "IEDriverServer.exe";
				System.setProperty("webdriver.ie.driver", bsPath);
				DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
				capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
				this.browserDriver = new InternetExplorerDriver(capabilities);
			}else if(browserType.equals("ie64")){
				//64位IE浏览器
				bsPath = bsPath + "IEDriverServer64.exe";
				System.setProperty("webdriver.ie.driver", bsPath);
				DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
				capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
				this.browserDriver = new InternetExplorerDriver(capabilities);
			}else if(browserType.equals("chrome")){
				System.setProperty("webdriver.chrome.driver",bsPath + "chromedriver.exe");
				this.browserDriver = new ChromeDriver();
			}else if(browserType.equals("safari")){
				
			}
			this.setBrowserType(browserType);
			initSelenium(appTestUrl);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void waitSec(long seconds) throws InterruptedException{
		seconds = seconds*1000;
		this.browserDriver.wait(seconds);
	}
	
	public void stopTestInstance(){
		this.selenium.stop();
	}

	/**
	 * @Title initSelenium(String appTestUrl)
	 * @Description WebDriver迁移到selenium执行测试
	 * @param appTestUrl
	 * @throws IOException 
	 */
	protected void initSelenium(String appTestUrl) throws IOException{
		this.selenium = new WebDriverBackedSelenium(this.browserDriver, appTestUrl);
		this.selenium.open(appTestUrl);
	}

	public void setBrowserDriver(WebDriver browserDriver) {
		this.browserDriver = browserDriver;
	}

	public WebDriver getBrowserDriver() {
		return browserDriver;
	}

	public void setBrowserType(String browserType) {
		this.browserType = browserType;
	}

	public String getBrowserType() {
		return browserType;
	}

	public Selenium getSelenium() {
		return selenium;
	}

	public void setAppTestUrl(String appTestUrl) {
		this.appTestUrl = appTestUrl;
	}

	public String getAppTestUrl() {
		return appTestUrl;
	}
}
  
    